package presentation.optionForm_UI;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;

import presentation.menuForm_UI.Picture;
import business.optionForm_Evt.OptionFrom_Evt;

public class OptionForm_UI extends JDialog {

        private Picture pic = new Picture();
        private JPanel panelButton = new JPanel();
        private JPanel panelExplain = new JPanel();
        private JPanel panelMain = new JPanel();
        private JButton btControls = new JButton("Controls");
        private JButton btStatics = new JButton("Statics");
        
        private OptionFrom_Evt event = new OptionFrom_Evt(this);
        
        private Picture introPic = new Picture();
        
        private static final long serialVersionUID = 1L;

        public OptionForm_UI(JFrame frame) {

                setPanelMainLayout();
                buttonEvent();
                picPanelEvent();
                pic.path = "src/Image/focused_control.png";
                this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

                this.setResizable(false);
                this.setTitle("Option");
                this.add(panelMain);
                this.setModal(true);
                this.pack();
                this.setLocationRelativeTo(null);
        }

        private void setPanelMainLayout() {
                setButtonLayout();
                setPanelExplainLayout();
                panelMain.setLayout(new GridBagLayout());

                GridBagConstraints cOption = new GridBagConstraints();
                cOption.gridx = 0;
                cOption.gridy = 0;
                cOption.anchor = GridBagConstraints.WEST;
                cOption.weightx = 1.0;
                panelMain.add(panelButton, cOption);

                GridBagConstraints cExplain = new GridBagConstraints();
                cExplain.gridx = 1;
                cExplain.gridy = 0;
                cExplain.anchor = GridBagConstraints.EAST;
                panelMain.add(panelExplain, cExplain);
        }

        private void setButtonLayout() {
                setButtons();
                panelButton.setLayout(null);
                btControls.setBounds(1000, 1000, 10, 10);
                panelButton.add(btControls);

                btStatics.setBounds(1010, 1010, 10, 10);
                panelButton.add(btStatics);
                panelButton.setPreferredSize(new Dimension(336, 536));
                panelButton.add(pic);
                pic.setBounds(0, 0, 336, 536);

        }

        private void setPanelExplainLayout() {
                panelExplain.setPreferredSize(new Dimension(336, 536));
                panelExplain.setLayout(null);
                introPic.path = "src/image/tooltips/controls.png";
                introPic.repaint();
                panelExplain.add(introPic);
                
                introPic.setBounds(0, 0, 337, 536);
        }

        private void setButtons() {

                btControls.setOpaque(false);
                btControls.setBorderPainted(false);
                btControls.setContentAreaFilled(false);

                btStatics.setOpaque(false);
                btStatics.setBorderPainted(false);
                btStatics.setContentAreaFilled(false);
        }

        private void buttonEvent() {
                btControls.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent e) {
                                event.playMouse_Pressed();

                                event.btControls_Evt();
                        }
                });

                btStatics.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent e) {
                                event.playMouse_Pressed();
                                event.btStatics_Evt();
                        }
                });

                btControls.addKeyListener(new KeyAdapter() {

                        @Override
                        public void keyPressed(KeyEvent evt) {
                                if (evt.getKeyCode() == KeyEvent.VK_UP || evt.getKeyCode() == KeyEvent.VK_DOWN || evt.getKeyCode() == KeyEvent.VK_LEFT || evt.getKeyCode() == KeyEvent.VK_RIGHT) {
                                	btStatics.requestFocus();
                                    pic.path = "src/Image/focused_statics.png";
                                    introPic.path = "src/Image/tooltips/statics.png";
                                    pic.repaint();
                                    introPic.repaint();
                                       
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
                                        setVisible(false);
                                }
                        }
                });

                btStatics.addKeyListener(new KeyAdapter() {

                        @Override
                        public void keyPressed(KeyEvent evt) {
                                if (evt.getKeyCode() == KeyEvent.VK_UP || evt.getKeyCode() == KeyEvent.VK_DOWN || evt.getKeyCode() == KeyEvent.VK_LEFT || evt.getKeyCode() == KeyEvent.VK_RIGHT) {
                                        btControls.requestFocus();
                                        pic.path = "src/Image/focused_control.png";
                                        introPic.path = "src/Image/tooltips/controls.png";
                                        pic.repaint();
                                        introPic.repaint();
                                       
                                }
                                if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
                                        setVisible(false);
                                }
                        }
                });
        }

        private void picPanelEvent() {
                pic.addMouseMotionListener(new MouseMotionAdapter() {
                        @Override
                        public void mouseMoved(MouseEvent evt) {
                                final int X = evt.getX();
                                final int Y = evt.getY();
                                if ((X >= 15 && X <= 15 + 158) && (Y >= 137 && Y <= 137 + 158)) {
                                        btStatics.requestFocus();
                                        pic.path = "src/Image/focused_statics.png";
                                        introPic.path = "src/Image/tooltips/statics.png";
                                        pic.repaint();
                                        introPic.repaint();
                                }
                                if ((X >= 18 && X <= 18 + 158) && (Y >= 300 && Y <= 300 + 158)) {
                                        btControls.requestFocus();
                                        pic.path = "src/Image/focused_control.png";
                                        introPic.path = "src/Image/tooltips/controls.png";
                                        pic.repaint();
                                        introPic.repaint();
                                }
                        }
                });

                pic.addMouseListener(new MouseAdapter() {

                        @Override
                        public void mousePressed(MouseEvent evt) {
                                final int X = evt.getX();
                                final int Y = evt.getY();
                                if ((X >= 15 && X <= 15 + 158) && (Y >= 137 && Y <= 137 + 158)) {
                                        btStatics.requestFocus();
                                        event.playMouse_Pressed();
                                        event.btStatics_Evt();
                                }
                                if ((X >= 18 && X <= 18 + 158) && (Y >= 300 && Y <= 300 + 158)) {
                                        btControls.requestFocus();
                                        event.playMouse_Pressed();
                                        event.btControls_Evt();
                                }
                        }
                });
        }
}
